//
//  ForgotPasswordViewController.m
//  FMS
//
//  Created by Steven on 10/18/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ForgotPasswordViewController ()

@end

@implementation ForgotPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
        
        [self addTopBar];
        if (self.navBar) {
            self.navBar.navBarViewDelegate = self;
            self.navBar.titleLabel.text = [dictLanguage objectForKey:@"forgot-password.title"];
            self.navBar.rightButton.hidden = YES;
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tfEmailAddress.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.tfEmailAddress.layer.borderWidth = 1.0;
    self.tfEmailAddress.placeholder=[dictLanguage objectForKey:@"forgot-password.email-address"];
    
    //set background for Sign in button
    [self.btnSubmit setBackgroundImage:[AppDelegate imageFromColor:APPLE_BLUE_COLOR] forState:UIControlStateNormal];
    [self.btnSubmit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
//    [self.btnSubmit setBackgroundImage:[AppDelegate imageFromColor:[UIColor whiteColor]] forState:UIControlStateHighlighted];
    [self.btnSubmit setTitleColor:APPLE_BLUE_COLOR forState:UIControlStateHighlighted];
    
    [self.btnSubmit setTitle:[dictLanguage objectForKey:@"forgot-password.btn-submit"] forState:UIControlStateNormal];
    self.tvMessageEmailAddress.text=[dictLanguage objectForKey:@"forgot-password.ms-content"];
    
    myActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    myActivityView.frame = CGRectMake(350, 5, 30, 30);
    myActivityView.hidesWhenStopped = YES;
    [myActivityView stopAnimating];
    [self.btnSubmit addSubview:myActivityView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changePassWord{
    UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"forgot-password.successfully"] message:[dictLanguage objectForKey:@"forgot-password.request-ok"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
    [dialog show];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnSubmitClick:(id)sender {
    [myActivityView startAnimating];
    self.btnSubmit.userInteractionEnabled = NO;
    
    NSString *messageError = nil;
    NSString *emailRegexString	= @"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    if ([self.tfEmailAddress.text isEqualToString:@""]) {
        messageError = [dictLanguage objectForKey:@"liaison.title-required-email"];
    }
    else if (![self.tfEmailAddress.text isMatchedByRegex:emailRegexString]) {
        messageError = [NSString stringWithFormat:@"%@",[dictLanguage objectForKey:@"liaison.title-invalid-email"]];// @"Invalid format email.\n Please input email again.";
    }
    if (messageError) {
        [self showErrorMessage:messageError];
        return;
    }
    [[FMSLib shareClient] forgotPasswordEmail:self.tfEmailAddress.text successs:^(NSMutableDictionary *response){
        [self changePassWord];
    } failure:^(NSError *error){
        [myActivityView stopAnimating];
        self.btnSubmit.userInteractionEnabled = YES;
        
        [[[UIAlertView alloc] initWithTitle:@"Warning!"
                                    message:[error.userInfo objectForKey:@"description"]
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }];
}
#pragma mark - NavViewDelegate

- (void)didClickedLeftButton {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didClickedRightButton{
    
}

@end
